package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper{

    @Autowired
    private IDBUtil dbUtil;

    @Override//

    public int insertUser(User user) {
        try (Connection connection = dbUtil.getConnection();){//获取数据库连接
            String sql = "insert into user(username,password,nickname,age) values(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);//创建预编译语句对象
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
    public  User selectUserByUsername(String username){
        try (Connection connection = dbUtil.getConnection();){//获取数据库连接
            String sql = "select * from user where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);//创建预编译语句对象
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
            return null;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public List<User> selectAllUser(){
        List<User> list = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection();) {
            String sql = "SELECT * FROM user";
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                list.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int updateUser(User  user){
        try (Connection connection = dbUtil.getConnection();){//获取数据库连接
            String sql = "UPDATE user SET password = ?,nickname = ?,age = ? WHERE id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);//创建预编译语句对象
            ps.setString(1, user.getPassword());
            ps.setString(2, user.getNickname());
            ps.setInt(3, user.getAge());
            ps.setInt(4, user.getId());
            return ps.executeUpdate();//执行更新操作
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
}
